package com.wwx.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wwx.dao.RankingDao;
import com.wwx.entity.CartoonDO;
import com.wwx.util.DBHelper;

/**
 * 排行榜Dao层 2017.10.03
 * 
 * @author Wyndem
 *
 */
public class RankingDaoImp implements RankingDao {


	private CartoonDaoImpl c = new CartoonDaoImpl();

	@Override
	public List<CartoonDO> ticketTop(int number) {
		Connection con = DBHelper.getConnection();
		try {
			// 遍历所有的月票表
			String sql = "SELECT cartoonid FROM t_ticket";
			PreparedStatement pst = con.prepareStatement(sql);
			ResultSet res = pst.executeQuery();

			// 存放所有月票的動漫ID
			List<Integer> cartoonId = new ArrayList<Integer>();

			// 將查到的數據存放到list中去
			while (res.next()) {
				int i = res.getInt("cartoonid");
				cartoonId.add(i);
			}

			// 存储id和他的月票总数
			HashMap<Integer, Integer> topMap = new HashMap<>();

			// 进行赛选和统计月票
			while (cartoonId.size() > 0) {
				List<Integer> rem = new ArrayList<>();
				int i = cartoonId.get(0);
				int cont = 0;
				for (Integer r : cartoonId) {
					if (r == i) {
						cont++;
						rem.add(r);
					}
				}

				cartoonId.removeAll(rem);

				topMap.put(i, cont);
			}

			// 获取从大到小的动漫值
			List<Integer> listKey = sortMap(topMap, new ArrayList<Integer>());

			// 定义了从大到小的排序漫画
			ArrayList<CartoonDO> top = new ArrayList<CartoonDO>();
			// 填充top变量
			for (Integer i : listKey) {
				if (top.size() == number)
					break;
				CartoonDO cartoonDO = c.byIdCartoon(i, con);

				top.add(cartoonDO);

			}
			
			return top;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			//关闭连接
			DBHelper.closeConnection(con);
		}
	}

	@Override
	public List<CartoonDO> clickTop(int number) {
		Connection con = DBHelper.getConnection();
		try {
			String sql = "SELECT cartoonid,infoclicks FROM t_info";
			PreparedStatement pst = con.prepareStatement(sql);
			ResultSet res = pst.executeQuery();
			// 存储id和他的总点击数
			HashMap<Integer, Integer> topMap = new HashMap<>();
			while (res.next()) {
				int id = res.getInt("cartoonid");
				int value = res.getInt("infoclicks");
				topMap.put(id, value);
			}
			// 获取从大到小的动漫值
			List<Integer> listKey = sortMap(topMap, new ArrayList<Integer>());
			// 定义了从大到小的排序漫画
			ArrayList<CartoonDO> top = new ArrayList<CartoonDO>();
			// 填充top变量
			for (Integer i : listKey) {

				if (top.size() == number)
					break;
				CartoonDO cartoonDO = c.byIdCartoon(i, con);

				top.add(cartoonDO);

			}
			return top;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}finally {
			//关闭连接
			DBHelper.closeConnection(con);
		}
	}

	@Override
	public List<CartoonDO> collectionTop(int number) {
		Connection con = DBHelper.getConnection();
		try {
			String sql = "SELECT cartoonid,infocollections FROM t_info";
			PreparedStatement pst = con.prepareStatement(sql);
			ResultSet res = pst.executeQuery();
			// 存储id和他的总点击数
			HashMap<Integer, Integer> topMap = new HashMap<>();
			while (res.next()) {
				int id = res.getInt("cartoonid");
				int value = res.getInt("infocollections");
				topMap.put(id, value);
			}
			// 获取从大到小的动漫值
			List<Integer> listKey = sortMap(topMap, new ArrayList<Integer>());
			// 定义了从大到小的排序漫画
			ArrayList<CartoonDO> top = new ArrayList<CartoonDO>();
			// 填充top变量
			for (Integer i : listKey) {

				if (top.size() == number)
					break;
				CartoonDO cartoonDO = c.byIdCartoon(i, con);

				top.add(cartoonDO);

			}
			return top;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<CartoonDO> ticketTop(String type, int number) {
		// 获取全部的月票排行榜
		List<CartoonDO> list = ticketTop(-1);
		// 创建同类型的排行榜
		ArrayList<CartoonDO> clist = new ArrayList<CartoonDO>();
		for (CartoonDO cartoonDO : list) {
			if (cartoonDO.getCartype().equals(type)) {
				clist.add(cartoonDO);
			}
		}
		return clist;
	}

	@Override
	public List<CartoonDO> clickTop(String type, int number) {
		// 先获取全部的
		List<CartoonDO> clickTop = clickTop(-1);
		// 创建同类型的排行榜
		ArrayList<CartoonDO> clist = new ArrayList<CartoonDO>();
		for (CartoonDO cartoonDO : clickTop) {
			if (cartoonDO.getCartype().equals(type)) {
				clist.add(cartoonDO);
			}
		}
		return clist;
	}

	@Override
	public List<CartoonDO> collectionTop(String type, int number) {
		// 先获取全部的
		List<CartoonDO> collectionTop = collectionTop(-1);
		// 创建同类型的排行榜
		ArrayList<CartoonDO> clist = new ArrayList<CartoonDO>();
		for (CartoonDO cartoonDO : collectionTop) {
			if (cartoonDO.getCartype().equals(type)) {
				clist.add(cartoonDO);
			}
		}
		return clist;
	}

	/**
	 * 返回从大到小的key
	 * 
	 * @param map
	 * @return
	 */
	private List<Integer> sortMap(Map<Integer, Integer> map, ArrayList<Integer> list) {
		int max = 0;
		int key = 0;
		ArrayList<Integer> keyList = list;
		for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
			Integer value = entry.getValue();
			if (max == 0) {
				max = value;
				key = entry.getKey();
			} else if (value > max) {
				max = value;
				key = entry.getKey();
			}
		}
		keyList.add(key);
		map.remove(key);
		if (map.size() == 0) {
			return keyList;
		}
		return sortMap(map, keyList);
	}

}
